/**
 * 投诉信息controller层
 */
package com.bys.ots.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bys.ots.model.Complaint;
import com.bys.ots.model.Email;
import com.bys.ots.pojo.ConstantModel;
import com.bys.ots.pojo.Result;
import com.bys.ots.pojo.ResultUtil;
import com.bys.ots.service.ComplaintService;
import com.bys.ots.service.MailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.net.URLDecoder;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

/**
 * @author bairuihua
 * 2019/12/03
 */
@Api(value = "Complaint Controller")
@RestController
@RequestMapping(value = "v1")

public class ComplaintController {
    @Resource
    private ComplaintService complaintService;
    @Resource
    private MailService mailService;
    @Value("${textPath}")
    private String textPath;

    private final Logger log = LoggerFactory.getLogger(ComplaintController.class);

    @ApiOperation(value = "get All Complaint")
    @GetMapping(value = "/getAllComplaint")
    public Result getAllComplaint() {

        log.info("->User Enter controller getAllComplaint ");
        return ResultUtil.OTSResult(complaintService.getAllComplaint());
    }

    @ApiOperation(value = "insert complaint")
    @PostMapping(value = "/insertComplaint")
    @ApiImplicitParams
            ({
                    @ApiImplicitParam(name = "rawData", paramType = "body", dataType = "Complaint", value = "请求体", required = true)
            })
    public Result insertComplaint(@RequestBody Complaint complaint) {

        log.info("->User Enter controller insertComplaint ");
        return ResultUtil.OTSResult(complaintService.insertComplaint(complaint));
    }


    @ApiOperation(value = "send complaint mail")
    @PostMapping(value = "/sendComplaintEmail")
    @ApiImplicitParams
            ({
                    @ApiImplicitParam(name = "rawData", paramType = "body", dataType = "Complaint", value = "请求体", required = true)
            })
    public Result sendComplaintEmail(@RequestBody String so) {

        log.info("->User Enter controller insertComplaint ");
        return ResultUtil.OTSResult(complaintService.sendComplaintEmail(so));
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws IOException {
        log.info("=== file type :" + file.getContentType() + "file name : " +
                file.getOriginalFilename() + " file size : {2}" + file.getSize());
        InputStream inputStream = file.getInputStream();
        Writer write = new BufferedWriter(
                new OutputStreamWriter(
                        new FileOutputStream(textPath + file.getOriginalFilename()), "UTF-8"));
        IOUtils.copy(inputStream, write);
        write.close();
        return ResultUtil.success();
    }

    @ApiOperation(value = "Files Upload and Code Conversion")
    @PostMapping("/upload1")
    public Result upload1(MultipartFile file) throws IOException {
        log.info("=== file type :" + file.getContentType() + "file name : " +
                file.getOriginalFilename() + " file size : {2}" + file.getSize());
        InputStream inputStream = file.getInputStream();
        InputStreamReader isr = new InputStreamReader(inputStream, "UTF-16LE");
        Writer writer = new BufferedWriter(
                new OutputStreamWriter(
                        new FileOutputStream(textPath + file.getOriginalFilename()), "UTF-16LE"));
        IOUtils.copy(isr, writer);
        writer.close();
        return ResultUtil.success();
    }

    @ApiOperation(value = "Excel Upload ")
    @PostMapping("/upload2")
    public Result upload2(MultipartFile file) throws IOException {
        log.info("=== file type :" + file.getContentType() + "file name : " +
                file.getOriginalFilename() + " file size : {2}" + file.getSize());
        //file.transferTo(new File("D:\\temptext\\" + file.getOriginalFilename()));
        file.transferTo(new File(textPath + file.getOriginalFilename()));

        return ResultUtil.success();
    }


    @ApiOperation(value = "checkRecord")
    @PostMapping(value = "/checkRecord")
    @ApiImplicitParams
            ({
                    @ApiImplicitParam(name = "json", paramType = "body", dataType = "JSON", value = "请求体", required = true)
            })
    public Result checkRecord(@RequestBody JSONObject json) {
        log.info("===checkRecord:" + json);
        complaintService.checkRecord(json);
        complaintService.sendEmail(json);
        return ResultUtil.success();
    }


}
